package com.hrt.datasender.datasent;

import java.util.List;

import com.btcode.common.MyLog;
import com.btcode.log.ILog;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.service.BcSendPlatformService;
import com.hrt.datasender.datasent.socket.SocketClient;
import com.hrt.datasender.datasent.socket.SocketClientManager;
import com.hrt.datasender.mq.MqKit;
import com.hrt.datasender.mq.model.MqModel;
import com.hrt.datasender.mq.model.PacketModel;
import com.hrt.datasender.web.operInfo.OperInfoService;

/**
 * Created by Chance on 2017/11/2.
 */
public class DataSender {

    public static ILog log;

    public static void start() {

        log = MyLog.getInstance().getLogger(DataSender.class.getName());

        try {
            startSocketClient();
            customerDataMQ();
            customerHeatBeatMQ();
            customerResponseMQ();
            customerReissueDataMQ();
            log.info("消息收发模块初始化成功");
        }
        catch (Exception e) {
            log.error("消息收发模块初始化失败", e);
            throw e;
        }
    }

    /**
     * 启动socket客户端
     */
    public static void startSocketClient() {
        List<BcSendPlatformModel> spms = BcSendPlatformService.getInstance().getModelList();

        for (BcSendPlatformModel spm : spms) {
            String ip = spm.getIp();
            String port = spm.getPort();

            SocketClient nettyClient = new SocketClient(ip, Integer.parseInt(port));

            SocketClientManager.getInstance().addClient(nettyClient);
        }

        SocketClientManager.getInstance().start();
    }

    /**
     * 消费数据消息，往外发送
     */
    public static void customerDataMQ() {

        MqKit.consumeMsgFromBizDataMQ(new MqKit.MsgListener() {
            @Override
            public void onReceive(MqModel mqModel) {
                try {

                    PacketModel model = (PacketModel) mqModel;
                    log.info(model.getIp() + "准备发送数据:" + model.getMsg());
                    boolean finishSend = SocketClientManager.getInstance().sendMsg(model);
                    if (finishSend) {
                        log.info(model.getIp() + "发送数据成功:" + model.getMsg());
                    }
                }
                catch (Exception e) {
                    log.error("发送业务消息包错误：", e);
                }
            }
        });
    }

    /**
     * 消费心跳消息，往外发送
     */
    public static void customerHeatBeatMQ() {

        MqKit.consumeMsgFromHeatBeatMQ(new MqKit.MsgListener() {
            @Override
            public void onReceive(MqModel mqModel) {
                try {

                    PacketModel model = (PacketModel) mqModel;

                    boolean finishSend = SocketClientManager.getInstance().sendMsg(model);

                    if (finishSend) {
                        log.info("发送心跳包成功：" + model.getMsg());
                    }
                } 
                catch (Exception e) {
                    log.error("发送心跳包错误：", e);
                }
            }
        });
    }

    /**
     * 消费反控消息，往外发送
     */
    public static void customerResponseMQ() {

        MqKit.consumeMsgFromRecriminateMQ(new MqKit.MsgListener() {
            @Override
            public void onReceive(MqModel mqModel) {
                try {
                    PacketModel model = (PacketModel) mqModel;
                    boolean finishSend = SocketClientManager.getInstance().sendMsg(model);

                    if (finishSend) {
                        log.info("发送反控消息成功：" + model.getMsg());
                    }
                }
                catch (Exception e) {
                    log.error("发送反控消息错误：", e);
                }
            }
        });
    }

    /**
     * 消费补发数据队列，往外发送
     */
    public static void customerReissueDataMQ() {

        MqKit.consumeMsgFromReissueDataMQ(new MqKit.MsgListener() {
            @Override
            public void onReceive(MqModel mqModel) {
                try {
                    PacketModel model = (PacketModel) mqModel;
                    boolean finishSend = SocketClientManager.getInstance().sendMsg(model);

                    if (finishSend) {

                        log.info("发送补发数据成功：" + model.getMsg());
                    }
                }
                catch (Exception e) {
                    log.error("发送补发数据错误：", e);
                }
            }
        });
    }


}
